﻿using NPOI.SS.Formula.Functions;
using Org.BouncyCastle.Crypto.Generators;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Permissions;
using System.Text;
using System.Threading.Tasks;

namespace EasyCore.UnifyResult.Results
{
    public class EasyApiResult : IEasyApiResult, IEasyApiErrorResult
    {
        /// <summary>
        /// 状态码
        /// </summary>
        public int StatusCode { get; set; }

        /// <summary>
        /// 提示信息
        /// </summary>
        public string? Message { get; set; }

        /// <summary>
        /// 详细的错误信息
        /// </summary>
        public Dictionary<string, object> ErrorData { get; set; }

        /// <summary>
        /// 时间戳
        /// </summary>
        public long Timestamp { get; set; } = DateTime.Now.ToFileTimeUtc();

        /// <summary>
        /// 默认构造函数
        /// </summary>
        public EasyApiResult()
        {
        }

        /// <summary>
        ///构造函数1
        /// </summary>
        /// <param name="statusCode"></param>
        /// <param name="message"></param>
        public EasyApiResult(int statusCode, string message, Dictionary<string, object> errorData = null)
        {
            StatusCode = statusCode;
            Message = message;
            ErrorData = errorData;            
        }

        /// <summary>
        /// 执行统一错误信息处理
        /// </summary>
        /// <param name="statusCode"></param>
        /// <param name="message"></param>
        /// <returns></returns>

        public static EasyApiResult Errors(int statusCode, string message = "服务端或客户端错误") {
            var result = new EasyApiResult();
            result.StatusCode = statusCode;
            result.Message = message;
            return result;
        }


        /// <summary>
        /// 数据未找到数据
        /// </summary>
        /// <param name="message"></param>
        /// <returns></returns>
        public static EasyApiResult NoContent(string message = "NoContent")
        {
            var result = new EasyApiResult();
            result.StatusCode = (int)EasyStatusCodes.NoContent;
            result.Message = message;
            return result;
        }

        /// <summary>
        /// 处理成功
        /// </summary>
        /// <param name="message"></param>
        /// <returns></returns>
        public static EasyApiResult Ok(string message = "Ok")
        {
            var result = new EasyApiResult();
            result.StatusCode = (int)EasyStatusCodes.Ok;
            result.Message = message;
            return result;
        }

        /// <summary>
        /// 未认证
        /// </summary>
        /// <param name="message"></param>
        /// <returns></returns>
        public static EasyApiResult Unauthorized(string message = "Unauthorized")
        {
            var result = new EasyApiResult();
            result.StatusCode = (int)EasyStatusCodes.Unauthorized;
            result.Message = message;
            return result;
        }

        /// <summary>
        /// 未找到
        /// </summary>
        /// <param name="message"></param>
        /// <returns></returns>
        public static EasyApiResult NotFound(string message = "NotFound")
        {
            var result = new EasyApiResult();
            result.StatusCode = (int)EasyStatusCodes.NotFound;
            result.Message = message;
            return result;
        }

        /// <summary>
        /// 错误请求
        /// </summary>
        /// <param name="message"></param>
        /// <returns></returns>
        public static EasyApiResult BadRequest(string message = "BadRequest")
        {
            var result = new EasyApiResult();
            result.StatusCode = (int)EasyStatusCodes.BadRequest;
            result.Message = message;
            return result;
        }
    }


    public class EasyApiResult<T> : IEasyApiResult<T>
    {
        /// <summary>
        /// 状态码
        /// </summary>
        public int StatusCode { get; set; } = (int)EasyStatusCodes.Ok;
        /// <summary>
        /// 提示信息
        /// </summary>
        public string? Message { get; set; } = "处理成功";
        /// <summary>
        /// 返回的数据
        /// </summary>
        public T? Data { get; set; }
        /// <summary>
        /// 时间戳
        /// </summary>
        public long Timestamp { get; set; } = DateTime.Now.ToFileTimeUtc();
        /// <summary>
        /// 默认构造函数
        /// </summary>
        public EasyApiResult()
        {

        }

        /// <summary>
        /// 传递数据的构造函数
        /// </summary>
        /// <param name="data"></param>
        public EasyApiResult(T? data)
        {
            Data = data;
        }

        /// <summary>
        /// 实现隐式转换：从非泛型类转换到泛型类【所有的结果都是返回泛型类：在该系统中】
        /// </summary>
        /// <param name="easylApiResult"></param>
        public static implicit operator EasyApiResult<T>(EasyApiResult easylApiResult)
        {
            return new EasyApiResult<T>()
            {
                StatusCode = easylApiResult.StatusCode,
                Message = easylApiResult.Message,
                Timestamp = easylApiResult.Timestamp,
            };
        }
    }
}
